package cn.sdormitory.job.evaluation;

import cn.hutool.core.date.DateUtil;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.evaluation.dao.WorkDayDao;
import cn.sdormitory.evaluation.dao.WorkMonthInterviewDao;
import cn.sdormitory.evaluation.entity.WorkMonthInterviewEntity;
import cn.sdormitory.evaluation.vo.WorkDayVO;
import cn.sdormitory.exam.service.ExamResultInfoService;
import cn.sdormitory.exam.vo.ExamInfoVO;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 每月执行一次，添加本月要访谈的学生
 */
public class WorkMonthInterviewJob extends QuartzJobBean {
    @Resource
    WorkDayDao workDayDao;
    @Resource
    private ExamResultInfoService examResultInfoService;
    @Resource
    private WorkMonthInterviewDao workMonthInterviewDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        //通过学生表去查询到有多少班级，班级信息，班主任信息
        List<WorkDayVO> workDayVOs = workDayDao.selectClassInfo();
        for (WorkDayVO workDayVO : workDayVOs) {
            ExamInfoVO vo = new ExamInfoVO();
            vo.setClassId(workDayVO.getId()+"");
            //查询班级学生
            List<BStudent> bStudents = examResultInfoService.queryClassStudents(vo);
            List<WorkMonthInterviewEntity> list = new ArrayList<>();
            for (BStudent bStudent : bStudents) {
                WorkMonthInterviewEntity entity = new WorkMonthInterviewEntity();
                entity.setClassId(workDayVO.getId());
                entity.setClassName(workDayVO.getClassName());
                entity.setStudentId(bStudent.getId());
                entity.setStudentName(bStudent.getStudentName());
                entity.setInterviewMonth(DateUtil.format(new Date(),"yyyy-MM"));
                entity.setRegistrant(workDayVO.getRegistrant());
                entity.setCreateBy("SYSTEM");
                entity.setCreatedTime(new Date());
                list.add(entity);
            }
            //批量新增待访谈记录
            workMonthInterviewDao.batchInsert(list);

        }
    }


}
